
package com.rt.schedulenew.utils.global;

import com.alibaba.fastjson.JSONObject;
import com.rt.schedulenew.utils.util.JsonUtil;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

/**
 * 返回结果工具类
 */
public class JsonResultUtil {
    public static JsonResult success() {
        return new JsonResult()
                .setStatus(ErrorCode.SUCCESS.getKey())
                .setMessage(ErrorCode.SUCCESS.getValue());
    }

    public static JsonResult success(String message) {
        return new JsonResult()
                .setStatus(ErrorCode.SUCCESS.getKey())
                .setMessage(message);
    }

    public static JsonResult success(Object result) {
        return new JsonResult()
                .setStatus(ErrorCode.SUCCESS.getKey())
                .setMessage(ErrorCode.SUCCESS.getValue())
                .setResult(result);
    }

    public static JsonResult success(String message, Object result) {
        return new JsonResult()
                .setStatus(ErrorCode.SUCCESS.getKey())
                .setMessage(message)
                .setResult(result);
    }

    public static JsonResult success(String status, String message, Object result) {
        return new JsonResult()
                .setStatus(status)
                .setMessage(message)
                .setResult(result);
    }

    public static JsonResult success(String message, Object result, PageVO page) {
        return new JsonResult()
                .setStatus(ErrorCode.SUCCESS.getKey())
                .setMessage(message)
                .setResult(result)
                .setPage(page);
    }

    public static JsonResult failure() {
        return new JsonResult()
                .setStatus(ErrorCode.FAIL.getKey())
                .setMessage(ErrorCode.FAIL.getValue());
    }

    public static JsonResult failure(String message) {
        return new JsonResult()
                .setStatus(ErrorCode.FAIL.getKey())
                .setMessage(message);
    }

    public static JsonResult failure(String status, String message) {
        return new JsonResult()
                .setStatus(status)
                .setMessage(message);
    }

    public static JsonResult failure(String status, String message, Object result) {
        return new JsonResult().setStatus(status).setMessage(message).setResult(result);
    }

    public static JsonResult buildListJsonResult(Object obj) {
        if (obj == null) {
            return null;
        }
        List list = new ArrayList();
        list.add(obj);
        return success(list);
    }

    public static JsonResult buildKeyValueJsonResult(String key, Object value) {
        if (key == null || "".equals(key)) {
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(key, value);
        return success(ErrorCode.SUCCESS.getValue(), map);
    }

    public static JsonResult buildKeyValueListJsonResult(String key, Object value) {
        if (key == null || "".equals(key)) {
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(key, value);
        return buildListJsonResult(map);
    }

    public static <T> T getResult(JsonResult jsonResult) {
        if (jsonResult.getResult() == null) {
            return null;
        }
        T t = (T) jsonResult.getResult();
        return t;
    }

    public static <T> List<T> getResultList(JsonResult jsonResult) {
        if (jsonResult.getResult() == null) {
            return null;
        }
        List<T> list = (List<T>) jsonResult.getResult();
        return list;
    }

    public static <T> T getResultList0(JsonResult jsonResult) {
        if (jsonResult.getResult() == null) {
            return null;
        }
        List<T> list = (List<T>) jsonResult.getResult();
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    public static List<?> getResultList(JsonResult jsonResult, Class<?> clazz) {
        if (jsonResult.getResult() == null) {
            return null;
        }
        String resultStr = JsonUtil.toJson(jsonResult.getResult());
        return (List<?>) JsonUtil.toList(resultStr, (Class) clazz);
    }

    public static Object getResultList0(JsonResult jsonResult, Class<?> clazz) {
        if (jsonResult.getResult() == null) {
            return null;
        }
        String resultStr = JsonUtil.toJson(jsonResult.getResult());
        List<?> list = (List<?>) JsonUtil.toList(resultStr, (Class) clazz);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    public static String getResultValueByKey(String jsonResultStr, String key) {
        JsonResult jsonResult = JsonUtil.toBean(jsonResultStr, JsonResult.class);
        return getResultValueByKey(jsonResult, key);
    }

    public static String getResultValueByKey(JsonResult jsonResult, String key) {
        List list = (List) jsonResult.getResult();
        if (list == null || list.size() <= 0) {
            return null;
        }
        JSONObject obj = JsonUtil.toJsonObj(JsonUtil.toJson(list.get(0)));
        if (obj.get(key) == null) {
            return null;
        }
        return obj.get(key).toString();
    }
}
